package com.firesys.setting.ui;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import com.firesys.ExitApp;
import com.firesys.MainActivity;
import com.firesys.R;
import com.firesys.dbhelper.SharedPreferenceUtil;
import com.firesys.dbhelper.XmlNameHelper;
import com.firesys.httpserver.RequestUrl;
import com.firesys.setting.db.SettingDao;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 系统设置
 * 
 * @author Administrator
 * 
 */
public class SettingsActivity extends Activity implements OnClickListener {

	public Button button1;// 检测
	public Button button2;// 保存
	public EditText edit1;// IP
	public EditText edit2;// 端口
	public EditText edit3;// 水司代码

	public SettingDao dao;

	public ProgressDialog pd;

	public SharedPreferenceUtil sh;

	private final static int DIALOG = 1;

	public InputMethodManager imm;
	
	public FinalHttp   http;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingsys_web);
		sh=new SharedPreferenceUtil(this);
		dao=new SettingDao(this);
		http=new FinalHttp();
		
	        imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		initUi();
	}

	// 初始化UI控件
	public void initUi() {
		button1 = (Button) findViewById(R.id.setting_web_check);
		button2 = (Button) findViewById(R.id.setting_web_ok);
		edit1 = (EditText) findViewById(R.id.setting_web_id);
		edit2 = (EditText) findViewById(R.id.setting_web_port);
		edit3 = (EditText) findViewById(R.id.setting_DivisionCode);
		String[] s = dao.queryWebSeting();
		edit1.setText(s[0]);
		edit2.setText(s[1]);
		edit3.setText(s[2]);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_web_check:
			if(!TextUtils.isEmpty(edit1.getEditableText().toString())&&!TextUtils.isEmpty(edit2.getEditableText().toString())&&
				       !TextUtils.isEmpty(edit3.getEditableText().toString())){
				String url="http://"+edit1.getEditableText().toString()+":"+edit2.getEditableText().toString()+
						"/"+edit3.getEditableText().toString()+RequestUrl.CheckConnect;
				System.out.println("--url---"+url);
				http.get(url, new AjaxCallBack<Object>() {

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
					     Toast.makeText(SettingsActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
						super.onFailure(t, errorNo, strMsg);
						 sh.insertValue(XmlNameHelper.CHECKSERVERCONNECT, false);
						    edit1.setText(sh.getValue(XmlNameHelper.IP));
						    edit2.setText(sh.getValue(XmlNameHelper.port));
						    edit3.setText(sh.getValue(XmlNameHelper.webName));
					}

					@Override
					public void onSuccess(Object t) {
						 Toast.makeText(SettingsActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
						    sh.insertValue(XmlNameHelper.CHECKSERVERCONNECT, true);
							sh.insertValue(XmlNameHelper.IP, edit1.getEditableText().toString());
							sh.insertValue(XmlNameHelper.port, edit2.getEditableText().toString());
							sh.insertValue(XmlNameHelper.webName, edit3.getEditableText().toString());
						super.onSuccess(t);
					}
					
					
				});
			}
			
			break;

		default:
			break;
		}
	}

	
	
}
